package com.atguigu.jvm;

public class RuntimeHighTest {

    public static void main(String[] args) {
        System.out.println("=====================Begin=========================");
        System.out.print("最大可申请的堆大小：Xmx=");
        System.out.println(Runtime.getRuntime().maxMemory() / 1024.0 / 1024 + "M");

        System.out.print("空闲(已申请但尚未使用)的堆大小：free mem=");
        System.out.println(Runtime.getRuntime().freeMemory() / 1024.0 / 1024 + "M");

        System.out.print("当前已申请的堆大小：total mem=");
        System.out.println(Runtime.getRuntime().totalMemory() / 1024.0 / 1024 + "M");

        System.out.println("==================First Allocated===================");
        byte[] b1 = new byte[5 * 1024 * 1024];
        System.out.println("5MB array allocated");

        System.out.print("空闲(已申请但尚未使用)的堆大小：free mem=");
        System.out.println(Runtime.getRuntime().freeMemory() / 1024.0 / 1024 + "M");

        System.out.print("当前堆大小：total mem=");
        System.out.println(Runtime.getRuntime().totalMemory() / 1024.0 / 1024 + "M");

        System.out.println("=================Second Allocated===================");
        byte[] b2 = new byte[10 * 1024 * 1024];
        System.out.println("10MB array allocated");

        System.out.print("空闲(已申请但尚未使用)的堆大小：free mem=");
        System.out.println(Runtime.getRuntime().freeMemory() / 1024.0 / 1024 + "M");

        System.out.print("当前堆大小：total mem=");
        System.out.println(Runtime.getRuntime().totalMemory() / 1024.0 / 1024 + "M");

        System.out.println("=====================OOM=========================");
        System.out.println("OOM预警!!!");
        System.gc();

        System.out.println("第一个 40M 数组");
        byte[] b3 = new byte[40 * 1024 * 1024];

        System.out.println("第二个 40M 数组");
        byte[] b4 = new byte[40 * 1024 * 1024];

        System.out.println("第三个 40M 数组");
        byte[] b5 = new byte[40 * 1024 * 1024];
    }

}
